package com.lwx.sys.controller;


import com.lwx.sys.constant.Constant;
import com.lwx.sys.domain.Dept;
import com.lwx.sys.service.DeptService;
import com.lwx.sys.utils.DataGridView;
import com.lwx.sys.utils.ResultObj;
import com.lwx.sys.utils.TreeNode;
import com.lwx.sys.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 部门管理
 * @author lwx
 * @since 2020-04-09
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 加载左侧的部门树
     * @param deptVo
     * @return
     */
    @RequestMapping("loadAllDeptTreeJson")
    public DataGridView loadAllDeptTreeJson(DeptVo deptVo){
        deptVo.setAvailable(Constant.AVAILABLE_TRUE);
        //查询所有可用部门
        List<Dept> allDept=deptService.queryAllDeptForList(deptVo);
        List<TreeNode> treeNodes =new ArrayList<>();
        for (Dept dept:allDept){
            Integer id = dept.getId();
            Integer pid = dept.getPid();
            String title = dept.getTitle();
            Boolean spread = dept.getOpen() == 1;
            treeNodes.add(new TreeNode(id,pid,title,spread));
        }
        return new DataGridView(treeNodes);
    }

    /**
     * 加载右侧部门表格的所有数据
     * @param deptVo
     * @return
     */
    @GetMapping("/loadAllDept")
    public DataGridView loadAllDept(DeptVo deptVo){
        return deptService.loadAllDept(deptVo);
    }

    /**
     * 删除部门信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteDept")
    public ResultObj deleteDept(Integer id){
        try {
            deptService.deleteDept(id);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @PostMapping("/addDept")
    public ResultObj addDept(Dept dept){
        try {
            dept.setCreatetime(new Date());
            deptService.addDept(dept);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新部门
     * @param dept
     * @return
     */
    @PostMapping("/updateDept")
    public ResultObj updateDept(Dept dept){
        try {
            deptService.updateDept(dept);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 检查当前部门节点是否存在子节点
     * @param id
     * @return
     */
    @GetMapping("/checkCurrentDeptHasChildren")
    public DataGridView checkCurrentDeptHasChildren(Integer id){
        Integer count = deptService.queryDeptCountByPid(id);
        return new DataGridView(count);

    }

    /**
     * 查询最大的部门号
     * @return
     */
    @GetMapping("/loadDeptMaxOrderNum")
    public DataGridView loadDeptMaxOrderNum(){
        Integer max = deptService.queryDeptMaxOrderNum();
        return new DataGridView(max+1);
    }

}

